/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.anwen.mongo.bson.MongoPlusDocument;
import com.anwen.mongo.conditions.interfaces.condition.CompareCondition;
import com.anwen.mongo.conditions.query.LambdaQueryChainWrapper;
import com.anwen.mongo.conditions.query.QueryWrapper;
import com.anwen.mongo.conditions.update.LambdaUpdateChainWrapper;
import com.anwen.mongo.model.PageResult;
import com.anwen.mongo.service.impl.ServiceImpl;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.excel.utils.ExcelUtil;
import com.comac.ins.common.json.utils.JsonUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.constant.enums.*;
import com.comac.ins.isfd.domain.IsfdAnalysisConfig;
import com.comac.ins.isfd.domain.IsfdAnalysisParameterConfig;
import com.comac.ins.isfd.domain.IsfdOperator;
import com.comac.ins.isfd.domain.IsfdOperatorParameterConfig;
import com.comac.ins.isfd.domain.bo.IsfdAnalysisParameterConfigBo;
import com.comac.ins.isfd.domain.bo.IsfdOperatorBo;
import com.comac.ins.isfd.domain.bo.IsfdOperatorParameterMapDeleteBo;
import com.comac.ins.isfd.domain.bo.query.IsfdOperatorQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdOperatorCountObjectVo;
import com.comac.ins.isfd.domain.vo.IsfdOperatorVo;
import com.comac.ins.isfd.domain.vo.IsfdRelatedOperatorInfoVo;
import com.comac.ins.isfd.service.*;
import lombok.extern.slf4j.Slf4j;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 算子service实现类
 *
 * @author hxloongs
 * @date 2024-7-9
 */
@Slf4j
@Service
public class IsfdOperatorServiceImpl extends ServiceImpl<IsfdOperator> implements IIsfdOperatorService, IIsfdMetadataImportService {

    @Autowired
    private IIsfdAnalysisConfigService iIsfdAnalysisConfigService;

    @Autowired
    private IIsfdOperatorParameterConfigService iIsfdOperatorParameterConfigService;

    @Autowired
    private IIsfdAnalysisParameterConfigService iIsfdAnalysisParameterConfigService;

    @Override
    public List<IsfdOperatorCountObjectVo> listCount() {
        List<IsfdOperatorCountObjectVo> result = new ArrayList<>();
        List<IsfdOperator> isfdOperatorList = this.list();

        // 1. 处理脏数据，如果 application_type material_type algorithm_type 任意一个为空，则不符合条件
        isfdOperatorList = isfdOperatorList.stream().filter(iol -> IsfdOperatorApplicationTypeEnum.forValue(iol.getApplicationType()) != null
            && IsfdOperatorMaterialTypeEnum.forValue(iol.getMaterialType()) != null
            && IsfdOperatorAlgorithmTypeEnum.forValue(iol.getAlgorithmType()) != null).toList();

        if (CollectionUtil.isNotEmpty(isfdOperatorList)) {
            // 2. 解析applicationType层
            Map<String, List<IsfdOperator>> applicationTypeMap = isfdOperatorList.stream().collect(Collectors.groupingBy(IsfdOperator::getApplicationType));
            for (String applicationType : applicationTypeMap.keySet()) {
                List<IsfdOperator> materialTypeList = applicationTypeMap.get(applicationType);
                IsfdOperatorCountObjectVo applicationTypeVo = new IsfdOperatorCountObjectVo();
                applicationTypeVo.setType(applicationType);
                applicationTypeVo.setName(IsfdOperatorApplicationTypeEnum.getDescByValue(applicationType));
                applicationTypeVo.setCount((long) materialTypeList.size());

                List<IsfdOperatorCountObjectVo> applicationTypeChildren = new ArrayList<>();
                // 3. 解析materialType层
                Map<String, List<IsfdOperator>> materialTypeMap = materialTypeList.stream().collect(Collectors.groupingBy(IsfdOperator::getMaterialType));
                for (String materialType : materialTypeMap.keySet()) {
                    List<IsfdOperator> algorithmTypeList = materialTypeMap.get(materialType);
                    IsfdOperatorCountObjectVo materialTypeVo = new IsfdOperatorCountObjectVo();
                    materialTypeVo.setType(materialType);
                    materialTypeVo.setName(IsfdOperatorMaterialTypeEnum.getDescByValue(materialType));
                    materialTypeVo.setCount((long) algorithmTypeList.size());

                    List<IsfdOperatorCountObjectVo> materialTypeChildren = new ArrayList<>();
                    // 4. 解析algorithmType层
                    Map<String, List<IsfdOperator>> algorithmTypeMap = algorithmTypeList.stream().collect(Collectors.groupingBy(IsfdOperator::getAlgorithmType));
                    for (String algorithmType : algorithmTypeMap.keySet()) {
                        IsfdOperatorCountObjectVo algorithmTypeVo = new IsfdOperatorCountObjectVo();
                        algorithmTypeVo.setType(algorithmType);
                        algorithmTypeVo.setName(IsfdOperatorAlgorithmTypeEnum.getDescByValue(algorithmType));
                        algorithmTypeVo.setCount((long) algorithmTypeMap.get(algorithmType).size());
                        materialTypeChildren.add(algorithmTypeVo);
                    }

                    materialTypeVo.setChildren(materialTypeChildren);
                    applicationTypeChildren.add(materialTypeVo);
                }

                applicationTypeVo.setChildren(applicationTypeChildren);
                result.add(applicationTypeVo);
            }
        }
        return result;
    }

    @Override
    public TableDataInfo<IsfdOperatorVo> queryPageList(IsfdOperatorQueryBo queryBo) {

        // 构造返回对象列表
        List<IsfdOperatorVo> resultList = new ArrayList<>();
        List<IsfdOperator> isfdOperatorList = new ArrayList<>();
        Integer pageNum = queryBo.getPageNum();
        Integer pageSize = queryBo.getPageSize();

        // 构造查询参数
        LambdaQueryChainWrapper<IsfdOperator> queryChainWrapper = this.lambdaQuery();
        if (PageQuery.DEFAULT_ASC.equals(queryBo.getIsAsc())) {
            queryChainWrapper.orderByDesc(IsfdOperator::getCreateTime);
        } else {
            queryChainWrapper.orderByAsc(IsfdOperator::getCreateTime);
        }
        queryChainWrapper.like(StringUtils.isNotBlank(queryBo.getOperatorName()), IsfdOperator::getOperatorName, queryBo.getOperatorName());
        queryChainWrapper.eq(StringUtils.isNotBlank(queryBo.getAlgorithmRealize()), IsfdOperator::getAlgorithmRealize, queryBo.getAlgorithmRealize());
        queryChainWrapper.in(CollectionUtil.isNotEmpty(queryBo.getMetadataManagementIds()), IsfdOperator::getMetadataManagementId, queryBo.getMetadataManagementIds());

        if (CollectionUtil.isNotEmpty(queryBo.getCalculateObjects())) {
            List<Bson> bsonList = new ArrayList<>();
            for (IsfdOperator.CalculateObject calculateObject : queryBo.getCalculateObjects()) {
                MongoPlusDocument mongoPlusDocument = new MongoPlusDocument();
                mongoPlusDocument.put("object_type", calculateObject.getObjectType());
                if (StringUtils.isNotBlank(calculateObject.getObjectName())) {
                    mongoPlusDocument.put("object_name", calculateObject.getObjectName());
                }
                bsonList.add(mongoPlusDocument);
            }
            queryChainWrapper.all("calculate_object", bsonList);
        }

        // 当存在type list 的数据的时候，将级联查询从列表中循环处理
        // 否则进行正常的mongodb分页查询
        Long total = null;
        Boolean needPageMemoryLimit = Boolean.FALSE;
        if (CollectionUtil.isNotEmpty(queryBo.getTypeList())) {
            AtomicInteger counter = new AtomicInteger(0);
            for (IsfdOperatorQueryBo.TypeList type : queryBo.getTypeList()) {
                // 第一次要让queryChainWrapper的条件加入进去
                // 除了第一次以外都是替换其中的value值
                if (counter.get() == 0) {
                    queryChainWrapper.eq(StringUtils.isNotBlank(type.getApplicationType()), IsfdOperator::getApplicationType, type.getApplicationType());
                    queryChainWrapper.eq(StringUtils.isNotBlank(type.getMaterialType()), IsfdOperator::getMaterialType, type.getMaterialType());
                    queryChainWrapper.eq(StringUtils.isNotBlank(type.getAlgorithmType()), IsfdOperator::getAlgorithmType, type.getAlgorithmType());
                } else {
                    List<CompareCondition> compareList = queryChainWrapper.getCompareList();
                    for (CompareCondition condition : compareList) {
                        String column = condition.getColumn();
                        if (IsfdOperatorTypeEnum.APPLICATION_TYPE.getValue().equals(column)) {
                            condition.setValue(type.getApplicationType());
                        }
                        if (IsfdOperatorTypeEnum.MATERIAL_TYPE.getValue().equals(column)) {
                            condition.setValue(type.getMaterialType());
                        }
                        if (IsfdOperatorTypeEnum.ALGORITHM_TYPE.getValue().equals(column)) {
                            condition.setValue(type.getAlgorithmType());
                        }
                    }
                }
                isfdOperatorList.addAll(this.list(queryChainWrapper));
                counter.incrementAndGet();
            }
            // 做时间倒序排序
            isfdOperatorList.sort(Comparator.comparing(IsfdOperator::getCreateTime).reversed());
            // 判断是否需要内存分页
            needPageMemoryLimit = Boolean.TRUE;
        } else {
            PageResult<IsfdOperator> page = this.page(queryChainWrapper, pageNum, pageSize);
            isfdOperatorList.addAll(page.getContentData());
            total = page.getTotalSize();
        }

        // 空集合直接返回
        if (CollectionUtil.isEmpty(isfdOperatorList)) {
            return TableDataInfo.build();
        }

        // 实体类装成vo返回
        resultList = MapstructUtils.convert(isfdOperatorList, IsfdOperatorVo.class);

        // 需要根据算法配置表 对算子进行是否使用的标记处理
        QueryWrapper<IsfdAnalysisConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(IsfdAnalysisConfig::getNodeType, IsfdAnalysisConfigNodeTypeEnum.OPERATOR.getValue());
        List<IsfdAnalysisConfig> isfdAnalysisConfigList = iIsfdAnalysisConfigService.list(queryWrapper);
        List<String> operatorIdList = isfdAnalysisConfigList.stream().map(IsfdAnalysisConfig::getOperatorRefId).distinct().toList();
        for (IsfdOperatorVo isfdOperatorVo : resultList) {
            if (operatorIdList.contains(isfdOperatorVo.get_id())) {
                isfdOperatorVo.setUsed(Boolean.TRUE);
            }
        }
        return TableDataInfo.buildToPageMemoryLimit(needPageMemoryLimit, resultList, pageNum, pageSize, total);
    }

    @Override
    public IsfdOperatorVo queryDetail(String operatorRefId) {
        IsfdOperator operator = this.getById(operatorRefId);
        if (operator == null) {
            return null;
        }
        return MapstructUtils.convert(operator, IsfdOperatorVo.class);
    }

    @Override
    public Boolean add(IsfdOperatorBo isfdOperatorBo) {
        LambdaQueryChainWrapper<IsfdOperator> queryChainWrapper = this.lambdaQuery();
        queryChainWrapper.eq(IsfdOperator::getOperatorName, isfdOperatorBo.getOperatorName());
        queryChainWrapper.eq(IsfdOperator::getMaterialType, isfdOperatorBo.getMaterialType());
        List<IsfdOperator> list = this.list(queryChainWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            throw new BaseException("该算子名称已经存在，请更改算子名！");
        }
        isfdOperatorBo.setCreateTime(new Date());
        IsfdOperator isfdOperator = MapstructUtils.convert(isfdOperatorBo, IsfdOperator.class);
        this.save(isfdOperator);
        //todo 往operator_parameter_config写数据
        return Boolean.TRUE;
    }

    @Override
    public Boolean update(IsfdOperatorBo isfdOperatorBo) {
        IsfdOperator isfdOperator = this.getById(isfdOperatorBo.get_id());
        if (isfdOperator == null) {
            throw new BaseException("修改的算子不存在，请确认后重试！");
        }
        isfdOperator.setOperatorDescription(isfdOperatorBo.getOperatorDescription());
        isfdOperator.setAlgorithmRealize(isfdOperatorBo.getAlgorithmRealize());
        isfdOperator.setAlgorithmType(isfdOperatorBo.getAlgorithmType());
        isfdOperator.setApplicationType(isfdOperator.getApplicationType());
        isfdOperator.setMaterialType(isfdOperatorBo.getMaterialType());
        isfdOperator.setCalculateObjects(isfdOperatorBo.getCalculateObjects());
        return this.saveOrUpdate(isfdOperator);
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids) {
        for (String id : ids) {
            QueryWrapper<IsfdAnalysisConfig> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(IsfdAnalysisConfig::getNodeType, IsfdAnalysisConfigNodeTypeEnum.OPERATOR.getValue());
            queryWrapper.eq(IsfdAnalysisConfig::getOperatorRefId, id);
            List<IsfdAnalysisConfig> isfdAnalysisConfigList = iIsfdAnalysisConfigService.list(queryWrapper);
            if (CollectionUtil.isNotEmpty(isfdAnalysisConfigList)) {
                throw new BaseException("算子id为： " + id + " 已经用于算法编排，不可以删除！");
            }
            // 删除算子
            this.removeById(id);
            // 删除该算子关联的算子参数配置
            LambdaUpdateChainWrapper<IsfdOperatorParameterConfig> updateChainWrapper = iIsfdOperatorParameterConfigService.lambdaUpdate();
            updateChainWrapper.eq(IsfdOperatorParameterConfig::getOperatorRefId, id);
            iIsfdOperatorParameterConfigService.remove(updateChainWrapper);
        }
        return Boolean.TRUE;
    }

    @Override
    public List<IsfdRelatedOperatorInfoVo> parameterOutputList(String operatorRefId, String analysisRefId) {
        List<IsfdRelatedOperatorInfoVo> resultList = new ArrayList<>();

        // 获得算法编排图中的id
        LambdaQueryChainWrapper<IsfdAnalysisConfig> lambdaQueryForId = iIsfdAnalysisConfigService.lambdaQuery();
        lambdaQueryForId.eq(IsfdAnalysisConfig::getAnalysisRefId, analysisRefId);
        lambdaQueryForId.eq(IsfdAnalysisConfig::getOperatorRefId, operatorRefId);
        IsfdAnalysisConfig isfdAnalysisConfig = iIsfdAnalysisConfigService.one(lambdaQueryForId);
        if (isfdAnalysisConfig == null) {
            return resultList;
        }
        String graphOperatorId = isfdAnalysisConfig.get_id();

        // 获得所有顺序流节点的信息
        LambdaQueryChainWrapper<IsfdAnalysisConfig> lambdaQueryForSequenceFlow = iIsfdAnalysisConfigService.lambdaQuery();
        lambdaQueryForSequenceFlow.eq(IsfdAnalysisConfig::getNodeType, IsfdAnalysisConfigNodeTypeEnum.SEQUENCE_FLOW.getValue());
        lambdaQueryForSequenceFlow.eq(IsfdAnalysisConfig::getAnalysisRefId, analysisRefId);
        lambdaQueryForSequenceFlow.eq(IsfdAnalysisConfig::getTargetRefId, graphOperatorId);
        List<IsfdAnalysisConfig> isfdAnalysisConfigListForSequenceFlow = iIsfdAnalysisConfigService.list(lambdaQueryForSequenceFlow);
        if (CollectionUtil.isEmpty(isfdAnalysisConfigListForSequenceFlow)) {
            throw new BaseException("请检查算法的编排图，确保顺序流已经编排！");
        }
        // 获取所有父节点的id
        List<String> parentNodeIdList = isfdAnalysisConfigListForSequenceFlow.stream().map(IsfdAnalysisConfig::getSourceRefId).toList();
        for (String parentNodeId : parentNodeIdList) {
            IsfdAnalysisConfig algorithmIsfdAnalysisConfig = iIsfdAnalysisConfigService.getById(parentNodeId);
            // 如果不是操作节点类型就跳过
            if (!IsfdAnalysisConfigNodeTypeEnum.OPERATOR.getValue().equals(algorithmIsfdAnalysisConfig.getNodeType())) {
                continue;
            }
            // 查询父节点算子具体信息，获取算子名称和路径
            String parentOperatorRefId = algorithmIsfdAnalysisConfig.getOperatorRefId();
            IsfdOperator parentIsfdOperator = this.getById(parentOperatorRefId);
            String operatorName = parentIsfdOperator.getOperatorName();
            String algorithmRealize = parentIsfdOperator.getAlgorithmRealize();
            // todo 通过 algorithmRealize 字段，获取 operatorFieldIdList
            //  python工程中主要根据 algorithmRealize 工程文件路径，截取algorithmRealize的父节点，
            //  通过importlib.import_module(path路径)反射的方式，并解析出 operator_field_id（算子参数field_id）这个集合参数。
            //  当前只能先mock一下 operatorFieldIdList
            if (CollectionUtil.isNotEmpty(getMockFieldIdMap().get(operatorName))) {
                IsfdRelatedOperatorInfoVo isfdRelatedOperatorInfoVo = new IsfdRelatedOperatorInfoVo();
                isfdRelatedOperatorInfoVo.setOperatorRefId(parentOperatorRefId);
                isfdRelatedOperatorInfoVo.setOperatorName(operatorName);
                isfdRelatedOperatorInfoVo.setOperatorFieldIdList(getMockFieldIdMap().get(operatorName));
                resultList.add(isfdRelatedOperatorInfoVo);
            }
        }
        return resultList;
    }

    private Map<String, List<String>> getMockFieldIdMap() {
        Map<String, List<String>> mockFieldIdMap = new HashMap<>();

        List<String> fieldIdList1 = new ArrayList<>();
        fieldIdList1.add("sigma_ot");
        fieldIdList1.add("sigma_it");
        fieldIdList1.add("sigma_oc");
        fieldIdList1.add("sigma_ic");
        String operatorName1 = "a_stress_frame_bar_panel_open_tension_compression_class";
        mockFieldIdMap.put(operatorName1, fieldIdList1);

        List<String> fieldIdList2 = new ArrayList<>();
        fieldIdList2.add("sigma_A_in");
        fieldIdList2.add("sigma_A_out");
        fieldIdList2.add("sigma_B_in");
        fieldIdList2.add("sigma_B_out");
        String operatorName2 = "w_stress_frame_bar_panel_tension_compression_class";
        mockFieldIdMap.put(operatorName2, fieldIdList2);

        List<String> fieldIdList3 = new ArrayList<>();
        fieldIdList3.add("Nx_skin_cr");
        fieldIdList3.add("Nxy_skin_cr");
        fieldIdList3.add("stress_mstr_cr");
        String operatorName3 = "a_strain_c929_fuselage_StiffenedPanel_StringerDetach";
        mockFieldIdMap.put(operatorName3, fieldIdList3);

        List<String> fieldIdList4 = new ArrayList<>();
        fieldIdList4.add("Nx_detach_skin");
        fieldIdList4.add("Ny_detach_skin");
        fieldIdList4.add("Nxy_detach_skin");
        fieldIdList4.add("P_str");
        fieldIdList4.add("A_str");
        String operatorName4 = "w_strain_c929_fuselage_StiffenedPanel_StringerDetach";
        mockFieldIdMap.put(operatorName4, fieldIdList4);

        return mockFieldIdMap;
    }

    @Override
    public Boolean parameterMapSave(IsfdAnalysisParameterConfigBo isfdAnalysisParameterConfigBo) {
        String fieldId = isfdAnalysisParameterConfigBo.getFieldId();
        // 检查是否在算法层级修改过该参数的路径等信息
        // 设计算法层参数路径等信息修改，"field_source"为"input"
        LambdaQueryChainWrapper<IsfdAnalysisParameterConfig> queryChainWrapper = iIsfdAnalysisParameterConfigService.lambdaQuery();
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getFieldSource, IsfdAnalysisParameterConfigFieldSourceEnum.INPUT.getValue());
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getAnalysisRefId, isfdAnalysisParameterConfigBo.getAnalysisRefId());
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getOperatorRefId, isfdAnalysisParameterConfigBo.getOperatorRefId());
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getFieldId, fieldId);
        IsfdAnalysisParameterConfig isfdAnalysisParameterConfigForInput = iIsfdAnalysisParameterConfigService.one(queryChainWrapper);
        if (isfdAnalysisParameterConfigForInput != null) {
            throw new BaseException("算法层修改过算子" + fieldId + "的参数信息，不支持继续配置关联上游参数！");
        }
        // 检查是否已存在该参数的上游算子配置
        // 涉及更新映射关系相关的参数，"field_source"为"operator_result"
        queryChainWrapper.getCompareList().forEach(c -> {
            if ("field_source".equals(c.getColumn())) {
                c.setValue(IsfdAnalysisParameterConfigFieldSourceEnum.OPERATOR_RESULT.getValue());
            }
        });
        IsfdAnalysisParameterConfig isfdAnalysisParameterConfigForOperatorResult = iIsfdAnalysisParameterConfigService.one(queryChainWrapper);
        if (isfdAnalysisParameterConfigForOperatorResult != null) {
            throw new BaseException("该算子" + fieldId + "已经配置了关联上游参数！");
        }
        // 涉及更新映射关系相关参数，"field_source"为"operator_result"
        IsfdAnalysisParameterConfig isfdAnalysisParameterConfig = MapstructUtils.convert(isfdAnalysisParameterConfigBo, IsfdAnalysisParameterConfig.class);
        isfdAnalysisParameterConfig.setFieldSource(IsfdAnalysisParameterConfigFieldSourceEnum.OPERATOR_RESULT.getValue());
        return iIsfdAnalysisParameterConfigService.save(isfdAnalysisParameterConfig);
    }

    @Override
    public Boolean parameterMapUpdate(IsfdAnalysisParameterConfigBo isfdAnalysisParameterConfigBo) {
        LambdaQueryChainWrapper<IsfdAnalysisParameterConfig> queryChainWrapper = iIsfdAnalysisParameterConfigService.lambdaQuery();
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getFieldSource, IsfdAnalysisParameterConfigFieldSourceEnum.OPERATOR_RESULT.getValue());
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getAnalysisRefId, isfdAnalysisParameterConfigBo.getAnalysisRefId());
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getFieldId, isfdAnalysisParameterConfigBo.getFieldId());
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getOperatorRefId, isfdAnalysisParameterConfigBo.getOperatorRefId());
        IsfdAnalysisParameterConfig isfdAnalysisParameterConfig = iIsfdAnalysisParameterConfigService.one(queryChainWrapper);
        if (isfdAnalysisParameterConfig == null) {
            throw new BaseException("该算法层级别的配置参数不存在，请确认后重试！");
        }
        isfdAnalysisParameterConfig.setFieldSource(IsfdAnalysisParameterConfigFieldSourceEnum.OPERATOR_RESULT.getValue());
        isfdAnalysisParameterConfig.setAnalysisRefId(isfdAnalysisParameterConfigBo.getAnalysisRefId());
        isfdAnalysisParameterConfig.setOperatorRefId(isfdAnalysisParameterConfigBo.getOperatorRefId());
        isfdAnalysisParameterConfig.setFieldId(isfdAnalysisParameterConfigBo.getFieldId());
        isfdAnalysisParameterConfig.setRelatedOperatorRefId(isfdAnalysisParameterConfigBo.getRelatedOperatorRefId());
        isfdAnalysisParameterConfig.setRelatedFieldId(isfdAnalysisParameterConfigBo.getRelatedFieldId());
        return iIsfdAnalysisParameterConfigService.updateById(isfdAnalysisParameterConfig);
    }

    @Override
    public Boolean parameterMapDelete(IsfdOperatorParameterMapDeleteBo isfdOperatorParameterMapDeleteBo) {
        LambdaUpdateChainWrapper<IsfdAnalysisParameterConfig> isfdAnalysisConfigLambdaUpdateChainWrapper = iIsfdAnalysisParameterConfigService.lambdaUpdate();
        isfdAnalysisConfigLambdaUpdateChainWrapper.eq(IsfdAnalysisParameterConfig::getAnalysisRefId, isfdOperatorParameterMapDeleteBo.getAnalysisRefId());
        isfdAnalysisConfigLambdaUpdateChainWrapper.eq(IsfdAnalysisParameterConfig::getOperatorRefId, isfdOperatorParameterMapDeleteBo.getOperatorRefId());
        isfdAnalysisConfigLambdaUpdateChainWrapper.eq(IsfdAnalysisParameterConfig::getFieldId, isfdOperatorParameterMapDeleteBo.getFieldId());
        isfdAnalysisConfigLambdaUpdateChainWrapper.eq(IsfdAnalysisParameterConfig::getFieldSource, IsfdAnalysisParameterConfigFieldSourceEnum.OPERATOR_RESULT.getValue());
        return iIsfdAnalysisParameterConfigService.remove(isfdAnalysisConfigLambdaUpdateChainWrapper);
    }

    @Override
    public String selectByTableName() {
        return IsfdMetadataTableNameEnum.OPERATOR_LIST.getValue();
    }

    @Override
    public Long excelImport(File excelImportFile, String metadataManagementId) {
        long successCount = 0L;

        try (InputStream is = new FileInputStream(excelImportFile)) {
            List<IsfdOperatorVo> targetVos = ExcelUtil.importExcel(is, IsfdOperatorVo.class);
            for (IsfdOperatorVo vo : targetVos) {
                IsfdOperatorBo bo = MapstructUtils.convert(vo, IsfdOperatorBo.class);
                try {
                    // 转换json array 字符串为对象
                    String calculateObjectsJsonArrayStr = vo.getCalculateObjectsJsonArrayStr();
                    List<IsfdOperator.CalculateObject> calculateObjects = JsonUtils.parseArray(calculateObjectsJsonArrayStr, IsfdOperator.CalculateObject.class);
                    bo.setCalculateObjects(calculateObjects);
                    // 设置元数据管理id
                    bo.setMetadataManagementId(metadataManagementId);
                    this.add(bo);
                    successCount++;
                } catch (Exception e) {
                    log.error("算法库算子数据导入失败", e);
                }
            }

        } catch (Exception e) {
            log.error("sheet文件转换为实体类失败", e);
            throw new BaseException("sheet文件转换为实体类失败");
        }

        return successCount;
    }
}
